package org.gaojianqiao.electronichandbook.database;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.gaojianqiao.electronichandbook.createtable.EquipChoiceCatalTab;
import org.gaojianqiao.electronichandbook.createtable.EquipChoiceTab;

import zyg.login.DataBas;

public class OperateEquipChoiceTab
{
	public ArrayList<EquipChoiceTab> EquipChoicelist(String bookName)
			throws SQLException, UnsupportedEncodingException
	{

		// TODO Auto-generated method stub

		String sql = "select * from " + bookName + " order by id";
		DataBas publicDataBase = new DataBas();
		publicDataBase.connectDB("data3");
		ResultSet rs = publicDataBase.stmt.executeQuery(sql);
		ArrayList<EquipChoiceTab> EquipChoicelist = new ArrayList<EquipChoiceTab>();
		while (rs.next())
		{
			EquipChoiceTab EquipChoice = new EquipChoiceTab();
			EquipChoice.setid(rs.getString("id"));
			EquipChoice.setequipname(rs.getString("catalogue"));
			EquipChoice.setmodelnumber(rs.getString("name"));
			EquipChoice.setdetail(rs.getString("detail"));
			// EquipChoice.setequipname(rs.getString("equipname"));
			EquipChoicelist.add(EquipChoice);
			// System.out.println(""+rs.getString("weapon_id")+rs.getString("weapon_title")+rs.getString("EquipChoice"));
		}
		publicDataBase.closeDB();

		return EquipChoicelist;
	}

	public void insert(ArrayList<EquipChoiceTab> str) throws SQLException,
			ClassNotFoundException
	{
		String sql = "insert into EquipChoiceTab values (?,?,?,?)";
		DataBas publicDataBase = new DataBas();
		Connection conn = publicDataBase.connectDB("public");
		PreparedStatement ps = conn.prepareStatement(sql);
		int i = 0;
		do
		{
			ps.setString(1, str.get(i).getid());
			ps.setString(2, str.get(i).getequipname());
			ps.setString(3, str.get(i).getmodelnumber());
			ps.setString(4, str.get(i).getdetail());

			ps.executeUpdate();
			++i;
		} while (i < str.size());
		ps.close();
		conn.close();

	}

	public void createtable( ) throws SQLException, ClassNotFoundException
	{

		String sql2 = "drop table if exists EquipChoiceTab";
		String sql3 = "create table EquipChoiceTab (id text,equipname text,modelnumber text,detail text)";
		DataBas publicDataBase = new DataBas();
		Connection conn = publicDataBase.connectDB("data3");
		Statement stat = conn.createStatement();
		conn.setAutoCommit(false);
		stat.executeUpdate(sql2);
		stat.executeUpdate(sql3);
		conn.commit();
		conn.close();
	}
}
